<?php
/**
 *
 * @author kiet_minh
 * @since 2013-08-12
 */
class Feed
{
    /**
     * Retrieve holiday data from google feed.
     *
     * params portions:
     *   type: type of data [atom (default), json, json-in-script]
     *   callback: callback javascript function name (using with type of json-in-script only)
     *   from_year: start year
     *   to_year: end year
     *
     * return options:
     *   url: return google feed url
     *   content: return data content depending on the type which params specify
     *   array: return data content as an array
     *   simple_array: return only holiday information as array which format as array({date} => {holiday title})
     *
     * json-in-script always returns google feed json url wrapping in a script tab.
     *
     * @param array $params
     * @param string $return
     * @return string|array
     */
    public function google_holiday_calendar($params, $return = 'array') {
        
        if (!key_exists('location', $params)) {
            show_error('Location is needed.');
        }
        
        if (!in_array($return, array('url', 'content', 'array', 'simple_array'))) {
            show_error('Wrong second param of ' . __CLASS__ . '::' . __METHOD__);
        }
        
        $feed_url = 'http://www.google.com/calendar/feeds/' . $params['location'] . '@holiday.calendar.google.com/public/full';
        
        $query = array();
        
        // Data return type
        if (key_exists('type', $params)) {
            if (!in_array($params['type'], array('json', 'json-in-script', 'atom'))) {
                show_error('Unsupported type: ' . $params['type']);
            }
            $query[] = 'alt=' . $params['type'];
        }
        
        // Specify year
        if (key_exists('from_year', $params) AND key_exists('to_year', $params)) {
            $query[] = 'start-min=' . $params['from_year'] . '-01-01&start-max=' . $params['to_year'] . '-12-31';
        }
        
        $feed_url .= (count($query) > 0) ? '?' . implode('&', $query) : '';
        
        if ($params['type'] == 'json-in-script') {
            if (!key_exists('callback', $params)) {
                show_error('Callback param is needed for type of "json-in-script"');
            } else {
                $feed_url .= '&' . 'callback=' . $params['callback'];
                return '<script src="' . $feed_url . '"></script>';
            }
        } else {
            if ($return == 'url') {
                return $feed_url;
            }
            
            if ($params['type'] == 'json') {
                if (!$content = file_get_contents($feed_url)) {
                    show_error($feed_url . ' is a wrong url.');
                }
                if ($return == 'content') {
                    return $content;
                }
                
                $content_array = json_decode($content, true);
                if ($return == 'array') {
                    return $content_array;
                }
                
                if ($return == 'simple_array') {
                    $content_simple_array = array();
                    foreach ($content_array['feed']['entry'] as $entry) {
                        foreach ($entry['gd$when'] as $when) {
                            $content_simple_array[$when['startTime']] = $entry['title']['$t'];
                        }
                    }
                    ksort($content_simple_array);
                    return $content_simple_array;
                }
            } elseif ($params['type'] == 'atom') {
                if (!$content_object = simplexml_load_file($feed_url)) {
                    show_error($feed_url . ' is a wrong url.');
                }
                
                $content = $content_object->asXML();
                if ($return == 'content') {
                    return $content;
                }
                
                $content_array = json_decode(json_encode((array) simplexml_load_string($content)), true);
                if ($return == 'array') {
                    return $content_array;
                }
            }
        }
            
    }
}

?>